package pro.lepers.portfolio {
	import fr.digitas.flowearth.conf.Conf;
	import fr.digitas.flowearth.debug.FPS;
	import fr.digitas.flowearth.debug.Recorder;
	import fr.digitas.flowearth.debug.Reload;

	import pro.lepers.portfolio.controls.KeyManager;
	import pro.lepers.portfolio.core.Bootstrap;
	import pro.lepers.portfolio.core.master;
	import pro.lepers.portfolio.view.misc.cursor.Cursor;

	import com.pixelbreaker.ui.osx.MacMouseWheel;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.system.System;

	/**
	 * @author plepers
	 */
	public class Main extends Sprite {

		public function Main() {
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.frameRate = 60;
			
			MacMouseWheel.setup(stage);
			KeyManager.start( stage );
			Cursor._cinit( stage );
			
			
			
			trace( "============== Porfolio ==============");
			
			// DEBUG =====
				stage.addChild( new Recorder() );
//				var fps : FPS = new FPS();
//				fps.addEventListener( MouseEvent.CLICK, function() : void { try {System.gc();} catch( e : Error) {}} );
//				stage.addChild( fps );
				Reload.activate( stage );
				
				
				
				stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
				
			// ===========
			
			_init( );
		}

		private function onKeyDown (event : KeyboardEvent) : void {
			if( event.charCode == "s".charCodeAt( 0 ) ) {
				if( master.downScaled ) master.upScale();
				else master.downScale();
			}
		}

		private function _init() : void {
			_grabParams();
			var boot : Bootstrap = new Bootstrap( );
			boot.addEventListener( Event.COMPLETE, onBootComplete );
			boot.run();
		}

		private function _run() : void {
			_site = new Site();
			stage.addChild( _site );
			
//			var fc : Sprite;
//				addChild( fc = new FpsCheck() );
//				fc.x = 40;
//				fc.y = 400;
		}

		private var _site : Site;
		
		
		// _______________________________
		private function _grabParams() : void {
			Conf.setProperty( "confFile", "./datas/conf.xml" );
			Conf.grabParam( loaderInfo );
		}

		private function onBootComplete (event : Event) : void {
			_run();
		}
	}
}
